<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    function person_display_addresses ($addresses)
    {
        $CI = & get_instance();
        
        $current_address = $addresses[0];
        $current_rolls = array();
        array_push ($current_rolls, $addresses[0]);
        
        $html = "<h3>Addresses</h3>";

        $html .= "<table>";
        $html .= "    <thead>";
        $html .= "    <tr>";
        $html .= "    <th width=\"50%\">Address</th>";
        $html .= "    <th width=\"50%\">Electoral Rolls</th>";
        $html .= "    </tr>";
        $html .= "    </thead>";
        $html .= "    <tbody>";
        
        
        $is_latest = TRUE;
        
        for ($i = 1; $i < count ($addresses); $i++)
        {
            if ($addresses[$i]->address_id == $current_address->address_id)
            {
                array_push ($current_rolls, $addresses[$i]);
            }
            else
            {
                $html .= output_current_address ($is_latest, $current_address, $current_rolls);
                
                $is_latest = FALSE;
                $current_address = $addresses[$i];
                array_push ($current_rolls, $addresses[$i]);
            }
        }
        
        $html .= output_current_address ($is_latest, $current_address, $current_rolls);
        
        $html .= "</tbody></table>";
        
        return $html;
    }
    
    function output_current_address ($is_latest, $current_address, $current_rolls)
    {
        $html = "<tr class=\"tr" . alternator('1', '2') . "\">";
                
        $html .= "<td><p>";
        
        
        foreach (address_multi_line ($current_address->address1, $current_address->address2, $current_address->address3, $current_address->address4, $current_address->address5, $current_address->postcode) as $line)
        {
            $html .= $line . "<br/>";
        }
        $html .= "</p></td>";
        
        
        $html .= "<td><p>";
        foreach ($current_rolls as $roll)
        {
            $html .= $roll->name . " (" . strftime ("%d %b %Y", strtotime ($roll->compile_date)) . ")<br/>";
        }
        $html .= "</p></td>";
        
        
        $html .= "</tr>";
        
        return $html;
    }

    function person_display_voting_area ($voting_record)
    {
        $html = "<h3>Voting Record</h3>";
        
        if (count ($voting_record) == 0)
        {
            $html .= "<p>None found</p>";
        }
        else
        {
            $html .= "<table>";
            $html .= "<tr>";
            $html .= "<th>Date</th>";
            $html .= "<th>Vote Type</th>";
            $html .= "</tr>";
            
            foreach ($voting_record as $vote)
            {
                $html .= "<tr>";
                $html .= "<td>";
                $html .= strftime ("%b %Y", strtotime ($vote->election_date)) . " (" . $vote->election_type . ")";
                $html .= "</td>";
                $html .= "<td>";
                $html .= $vote->voted;
                $html .= "</td>";
                $html .= "</tr>";
            }
                    
            $html .= "</table>";
        }
        
        return $html;
    }

    function person_display_contacts ($contacts)
    {
        $html = "<h3>Contact</h3>";
        
        if (count($contacts) == 0)
        {
            $html .= "<p>None found</p>" ;
        }
        else
        {
            $html .= "<table>";
            $html .= "<tr>";
            $html .= "<th>Date</th>";
            $html .= "<th>Canvasser</th>";
            $html .= "<th>Party</th>";
            $html .= "<th>Notes</th>";
            $html .= "</tr>";
            
            foreach ($contacts as $contact)
            {
                $html .= "<tr class=\"tr" . alternator('1', '2') . "\">";
                $html .= "<td>";
                $html .= strftime ("%d %b %Y", strtotime ($contact->contact_date));
                $html .= "</td>";
                $html .= "<td>";
                $html .= $contact->contacted_by;
                $html .= "</td>";
                $html .= "<td>";
                $html .= $contact->party;
                if (!is_null($contact->rank))
                {
                    $html .= " (" . $contact->rank . ")";
                }
                $html .= "</td>";
                $html .= "<td>";
                $html .= $contact->notes;
                $html .= "</td>";
                $html .= "</tr>";
            }
                    
            $html .= "</table>";
        }
        
        return $html;
    }    
    
    function person_display_aliases ($aliases, $roll)
    {
        $html = "";
        
        if (count ($aliases) > 0)
        {
            $html .= "<h3>Aliases</h3>";
        
            $html .= "<table>";
            $html .= "<tr>";
            $html .= "<th>Alias</th>";
            $html .= "<th>Confirmed</th>";
            $html .= "<th>Notes</th>";
            $html .= "</tr>";
            
            foreach ($aliases as $alias)
            {
                $html .= "<tr>";
                $html .= "<td>";
                
                $html .= anchor("person/display/$alias->id/$roll", $alias->surname . ", " . $alias->first_names);
                
                $html .= "</td>";
                $html .= "<td>";
                $html .= $alias->confirmed;
                $html .= "</td>";
                $html .= "<td>";
                $html .= $alias->notes;
                $html .= "</td>";
                $html .= "</tr>";
            }
                    
            $html .= "</table>";
        }
        
        return $html;
    }    
    
?>